<template>
    <div class="goodsCard">
       <transition 
             v-on:before-enter="beforeEnter"
            v-on:enter="enter"
            v-on:after-enter="afterEnter"
       >
           
            <div class="ball" v-show="ball" ref="ball_rect"></div>
       </transition>
       <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <swiper :resource="getlunlist" :isfull="false"></swiper>
                </div>
            </div>
		</div>
        <!-- card身体部分 -->
		<div class="mui-card">
            <div class="mui-card-header">{{card_list.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>
                        市场价：<del>￥{{card_list.sell_price}}</del> <span class="now_price">￥{{card_list.market_price}}</span>
                    </p>
                   <!-- 定义 @getselect事件子向父传值，就用事件机制，子调用父的方法，传参数（值）进去，就实现子传父                -->
                   <p>购买数量：<numbox @getselect="getselectnumber" :max="card_list.stock_quantity"></numbox></p>
                   <!-- 想要子组件传值给父组件，
                   首先父组件向子组件传递方法，子组件调用这个方法时传参给方法，
                   这样父组件就有了子组件的值 -->
                    <p>
                        <mt-button size="normal" type="primary" >立即购买</mt-button>
                        <mt-button size="normal" type="danger" @click="add_shopcar">加入购物车</mt-button>
                    </p>
                </div>
            </div>
		</div>
        <div class="mui-card">
            <div class="mui-card-header">{{card_list.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品货号：{{card_list.goods_no}}</p>
                    <p>库存情况：{{card_list.stock_quantity}}件</p>
                    <p>上架时间：{{card_list.add_time | dataFormat}}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain @click="godec(id)">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain @click="gosel(id)">商品评论</mt-button>
            </div>
		</div>
    </div>
</template>
<script>
import swiper from "../subcomponent/swiper.vue"
import numbox from "../subcomponent/number.vue"
export default {
    data(){
        return {
            getlunlist:[],
            id:this.$route.params.id,
            card_list:{},
            ball:false,
            selectcount:1,
            
            
        }
    },
    created(){
         this.getlun();
         this.getcard();
    },
    methods:{
       getlun(){
           this.$http.get("api/getthumimages/"+this.id).then(result =>{
               if(result.body.status===0){
                   result.body.message.forEach(item =>{
                       item.img = item.src
                   })
                   this.getlunlist = result.body.message;
                   
               }
           });
          
       },

       getcard(){
           this.$http.get("api/goods/getinfo/"+this.id).then(result =>{
               if(result.body.status===0){
                   this.card_list = result.body.message[0]
               }
           })
       },
       //编程式路由
       godec(id){
           this.$router.push({name:"goodsdec",params:{id}})
              
          
       },
       gosel(id){
           this.$router.push({name:"gosel",params:{id}})
       },
        //点击购物车出出现小球
        add_shopcar(){
            this.ball = !this.ball
            var getinfo = {id:this.id,count:this.selectcount,price:this.card_list.sell_price,selected:true}
            // 调用了vuex里的mutation里的方法
            this.$store.commit('modifyCar',getinfo)//类似子组件调用父组件方法 this.$emit('方法'，参数)
           
        },
        // 半场动画函数
          beforeEnter: function (el) {
            //   0，0代表小球的初始位置
                el.style.transform = "translate(0,0)"
            },
            // 当与 CSS 结合使用时
            // 回调函数 done 是可选的
            enter: function (el, done) {
                el.offsetWidth
                // 通过getBoundingClientRect()获取元素在屏幕上位置，就可以得出小球要移动的x,y的值
                const rectObject1 = this.$refs.ball_rect.getBoundingClientRect();
                const rectObject2 = document.getElementById("num_car").getBoundingClientRect();
                const disty = rectObject2.top-rectObject1.top;
                const distx = rectObject2.left-rectObject1.left
                
                el.style.transform = `translate(${distx}px,${disty}px)`
                el.style.transition = 'all 0.1s ease'
                done()
            },
            afterEnter: function (el) {
                this.ball = !this.ball

            },
            // 获取购买数量的里数据
            getselectnumber(count){
                this.selectcount = count;
                console.log("获取到的选择的数字"+this.selectcount);
            }
    },
    components:{
        swiper:swiper,
        numbox
    }
}
</script>
<style lang="scss" scoped>
    .goodsCard {
        background-color:#ccc;
        overflow: hidden;
        .mui-card-footer{
            display: block;
            button {
                margin:10px 0px;
            }
        } 
        // 小球
        .ball {
            width:20px;
            height:20px;
            border-radius:50%;
            background-color:red;
            position: absolute;
            z-index: 50;
            top:385px;
            left:145px;
            
        }   
            
    }
</style>